//
//  LZFTopicCell.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/2.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFTopicCell.h"
#import "LZFTopicItem.h"
#import <UIImageView+WebCache.h>
#import "UIImage+Circle.h"

#import "LZFVedioView.h"
#import "LZFVoiceView.h"
#import "LZFPicView.h"

#import "LZFCommentItem.h"
#import "LZFUserItem.h"

@interface LZFTopicCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *createTime;
@property (weak, nonatomic) IBOutlet UILabel *text_Label;
@property (weak, nonatomic) IBOutlet UILabel *commentLabel;

@property (weak, nonatomic) IBOutlet UIView *hotComView;


@property (weak, nonatomic) IBOutlet UIButton *loveBtn;

@property (weak, nonatomic) IBOutlet UIButton *hateBtn;

@property (weak, nonatomic) IBOutlet UIButton *shareBtn;

@property (weak, nonatomic) IBOutlet UIButton *commentBtn;

@property(nonatomic,weak)LZFVedioView *vedioView;
@property(nonatomic,weak)LZFVoiceView *voiceView;
@property(nonatomic,weak)LZFPicView *picView;

@end

@implementation LZFTopicCell


-(LZFVedioView * )vedioView
{
    if (_vedioView==nil) {
        _vedioView=(LZFVedioView *)[LZFVedioView loadXibView];
        [self.contentView addSubview:_vedioView];
        self.vedioView=_vedioView;
    }
    return _vedioView;
}

-(LZFVoiceView * )voiceView
{
    if (_voiceView==nil) {
        _voiceView=(LZFVoiceView *)[LZFVoiceView loadXibView];
       [self.contentView addSubview:_voiceView];
         self.voiceView=_voiceView;
    }
    return _voiceView;
}

-(LZFPicView * )picView
{
    if (_picView==nil) {
        _picView=(LZFPicView *)[LZFPicView loadXibView];
      [self.contentView addSubview:_picView];
       self.picView=_picView;
    }
    return _picView;
}



-(void)setTopicItem:(LZFTopicItem *)topicItem
{
    _topicItem=topicItem;
    
    
    //icon
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:topicItem.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image==nil) return ;
        self.iconImageView.image=[image circleImage];
        
    }];
    
    //name
    self.nameLabel.text=topicItem.name;
    
    //createtime
    self.createTime.text=topicItem.create_time;
    
    //text
    self.text_Label.text=topicItem.text;
    
    
    //lovebtn hatebtn commentbtn sharebtn
    [self setButtonTitle:self.loveBtn  count:topicItem.love];
    [self setButtonTitle:self.hateBtn  count:topicItem.hate];
    [self setButtonTitle:self.shareBtn  count:topicItem.repost];
    [self setButtonTitle:self.commentBtn  count:topicItem.comment];
    
    //中间view
    switch (_topicItem.type) {
            //vedio
        case LZFTopicTypeVedio:
            self.vedioView.hidden=NO;
//            self.vedioView.frame=_topicItem.middleFrame;
            self.vedioView.topicItem=_topicItem;
            self.voiceView.hidden=YES;
            self.picView.hidden=YES;
            break;
            
        case LZFTopicTypeVoice:
            self.vedioView.hidden=YES;
//            self.voiceView.frame=_topicItem.middleFrame;
            self.voiceView.topicItem=_topicItem;
            self.voiceView.hidden=NO;
            self.picView.hidden=YES;
            break;
            
        case LZFTopicTypePic:
            self.vedioView.hidden=YES;
//            self.picView.frame=_topicItem.middleFrame;
            self.picView.topicItem=_topicItem;
            self.voiceView.hidden=YES;
            self.picView.hidden=NO;
            break;
            
        default:
            break;
    }

    
    //最热评论
   // LZFLog(@"--xrr%@",topicItem.topComment);
    if (topicItem.topComment) {
      //有评论
        self.hotComView.hidden=NO;
//        NSDictionary *cmt=topicItem.top_cmt[0];
//        NSString *content=cmt[@"content"];
//        NSString *username=cmt[@"user"][@"username"];
        NSString *content=topicItem.topComment.content;
        NSString *username=topicItem.topComment.user.username;
        
        if (content.length==0) {
            //语音评论
            self.commentLabel.text=[NSString stringWithFormat:@"语音评论:%@",username];
        }
        else
        {
            //非语音评论
            self.commentLabel.text=[NSString stringWithFormat:@"%@:%@",username,content];
        }

           }
    else
    {
        //无评论
         self.hotComView.hidden=YES;
    }
    
 }

//按钮数量的处理
-(void)setButtonTitle:(UIButton *)button  count:(NSInteger)count
{
   
    NSString *countText;

    if (count>10000.0) {
        countText=[NSString stringWithFormat:@"%0.1f万",count/10000.0];
    }
    
    else if (count>0)
    {
        countText=[NSString stringWithFormat:@"%ld",count];
    }
    else
    {
        countText=@"";
    }
    
    [button setTitle:countText forState:UIControlStateNormal];
}

//自定义分割线
-(void)setFrame:(CGRect)frame
{
    frame.origin.y+=10;
    frame.size.height-=10;
    [super setFrame:frame];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    /*
                LZFTopicTypeAll=1,
                LZFTopicTypePic=10,
                LZFTopicTypeWord=29,
                LZFTopicTypeVoice=31,
                LZFTopicTypeVedio=41
     
     */
    
    /*
    switch (_topicItem.type) {
        //vedio
        case LZFTopicTypeVedio:
            self.vedioView.hidden=NO;
            self.vedioView.frame=_topicItem.middleFrame;
            self.vedioView.topicItem=_topicItem;
            self.voiceView.hidden=YES;
            self.picView.hidden=YES;
        break;
            
        case LZFTopicTypeVoice:
            self.vedioView.hidden=YES;
            self.voiceView.frame=_topicItem.middleFrame;
            self.voiceView.topicItem=_topicItem;
            self.voiceView.hidden=NO;
            self.picView.hidden=YES;
            break;
            
        case LZFTopicTypePic:
            self.vedioView.hidden=YES;
            self.picView.frame=_topicItem.middleFrame;
            self.picView.topicItem=_topicItem;
            self.voiceView.hidden=YES;
            self.picView.hidden=NO;
            break;
            
        default:
            break;
    }
    */
    
    //因为layoutSubviews会被调用多次,所以其他的一些设置,放到模型的set方法里面去做
    switch (_topicItem.type) {
            //vedio
        case LZFTopicTypeVedio:
            self.vedioView.frame=_topicItem.middleFrame;
            break;
            
            //voice
        case LZFTopicTypeVoice:
            self.voiceView.frame=_topicItem.middleFrame;
            break;
            
            //pic
        case LZFTopicTypePic:
            self.picView.frame=_topicItem.middleFrame;
            break;
            
        default:
            break;
    }
}

@end
